package com.orange.base.component.biztool.simulateddata.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Random;

import com.orange.base.component.biztool.simulateddata.service.SimulatedDataEnum;
import com.orange.base.component.biztool.simulateddata.service.SimulatedDataParam;

/**
 * 文章生成器
 *
 * @author biankudingcha
 * @date: 2022-08-24
 * @since 0.5.0
 */
public class BullShitStrategy extends AbstractJson {

    @Override
    public Object getValue(SimulatedDataParam param) {
        String title = "文章生成";
        int length = 500;
        if (param.getValue() != null && !"".equals(param.getValue())) {
            title = param.getValue();
        }
        if (param.getMax() != null && !"".equals(param.getMax())) {
            length = Integer.valueOf(param.getMax());
        }
        return generate(title, length);
    }

    /**
     * @param title
     *            标题
     * @param length
     *            正文长度
     * @return content 返回正文长度
     */
    @SuppressWarnings("unchecked")
    public String generate(String title, int length) {
        // 解析字符串
        Map<String, Object> jsonData = (Map<String, Object>) super.getData(null, "map");
        String content = "";
        List<String> famous = (List<String>) jsonData.get("famous");
        List<String> before = (List<String>) jsonData.get("before");
        List<String> after = (List<String>) jsonData.get("after");
        List<String> bosh = (List<String>) jsonData.get("bosh");
        while (content.length() < length) {
            // 获取100以内的随机整数
            Random random = new Random();
            int rand = random.nextInt(100);
            if (rand < 10) {
            } else if (rand < 20) {
                content += ((String) famous.get(random.nextInt(famous.size())))
                    .replace("a", (String) before.get(random.nextInt(before.size())))
                    .replace("b", (String) after.get(random.nextInt(after.size())));
            } else {
                content += ((String) bosh.get(random.nextInt(bosh.size())));
                content = content.replace("x", title);
            }
        }

        return content;
    }

    @Override
    public String getFileName() throws Exception {
        return "BullShitData.json";
    }

    public String getType() {
        return SimulatedDataEnum.BULLSHIT.getName();
    }

    public String getCaption() {
        return SimulatedDataEnum.BULLSHIT.getCaption();
    }

}
